package fpt.edu.vn.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.UsersDao;
import fpt.edu.vn.model.Users;

public class AdminUpdateUserAction extends ActionSupport implements
		ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File userImage;
	private String userImageContentType;
	private String userImageFileName;
	private String username;
	private int reglevel;
	private String displayName;
	private int banned;

	UsersDao userdao = new UsersDao();

	Map<String, Object> session;

	private int usersId;

	private HttpServletRequest servletRequest;

	public String execute() {
		session = ActionContext.getContext().getSession();
 		if (session.containsKey("userRoleIdss")) {
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1) {
				try {
					Users user = new Users();
					user.setUsersId(usersId);
					user.setDisplayName(displayName.trim());
					user.setIsBanner(banned);
					user.setRoleId(reglevel);
					if(banned==1){
						user.setRoleId(2);
					}
					
					if (userImage != null) {
						String filePath = servletRequest.getRealPath("/");
						File fileToCreate = new File(filePath,
								this.userImageFileName);
						FileUtils.copyFile(this.userImage, fileToCreate);
						user.setAvatar(userImageFileName);
					}
					userdao.AdminUpdateUser(user);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public int getBanned() {
		return banned;
	}

	public void setBanned(int banned) {
		this.banned = banned;
	}

	public int getReglevel() {
		return reglevel;
	}

	public void setReglevel(int reglevel) {
		this.reglevel = reglevel;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}

	public String getUsername() {
		return username;
	}
	

	public int getUsersId() {
		return usersId;
	}

	public void setUsersId(int usersId) {
		this.usersId = usersId;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
